package com.xqb.listen.ui.splash

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateValue
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.xqb.listen.R
import com.xqb.listen.navigation.NavParams
import com.xqb.listen.ui.theme.SplashFont
import com.xqb.listen.ui.theme.White50
import com.xqb.listen.ui.theme.splash_icon_color
import kotlinx.coroutines.delay

/**
 * author:17873
 * date:2024/4/2
 * des:SplashPage
 **/

@Composable
fun SplashPage(onSkipPage : (navParams: NavParams)-> Unit) {

    LaunchedEffect(Unit) {
        delay(5 * 1000)
        onSkipPage(NavParams(NavParams.Router.MAIN))
    }

    val animaTransition = rememberInfiniteTransition(label = "startAnim")

    val rotateAnim by animaTransition.animateFloat(
        initialValue = 0f,
        targetValue = 360f,
        animationSpec = infiniteRepeatable(
            animation = tween(3000 , easing = LinearEasing),
        ),
        label = ""
    )

    val maskScaleAnim by animaTransition.animateValue(
        initialValue = 90.dp,
        targetValue = 250.dp,
        typeConverter = Dp.VectorConverter,
        animationSpec = infiniteRepeatable(
            animation = tween(2500 , easing = LinearEasing),
        ),
        label = ""
    )
    val alphaAnim by animaTransition.animateFloat(
        initialValue = 0.6f,
        targetValue = 0f,
        animationSpec = infiniteRepeatable(
            animation = tween(2500 , easing = LinearEasing)
        ),
        label = ""
    )
    val iconScaleAnim by animaTransition.animateValue(
        initialValue = 95.dp,
        targetValue = 140.dp,
        typeConverter = Dp.VectorConverter,
        animationSpec = infiniteRepeatable(
            animation = tween(2000 , easing = LinearEasing),
            repeatMode = RepeatMode.Reverse
        ),
        label = ""
    )



    Box(
        Modifier.fillMaxSize()
    ){
        Box(
            Modifier
                .size(300.dp)
                .align(Alignment.Center)
                .padding(bottom = 140.dp)
        ){
            Image(
                painter = painterResource(id = R.drawable.img_start_anim),
                contentDescription = null,
                colorFilter = ColorFilter.tint(splash_icon_color),
                modifier = Modifier
                    .size(maskScaleAnim)
                    .alpha(alphaAnim)
                    .graphicsLayer {
                        rotationZ = rotateAnim
                    }
                    .align(Alignment.Center)
            )
            Box(
                Modifier
                    .align(Alignment.Center)
                    .size(iconScaleAnim)
                    .background(
                        splash_icon_color,
                        shape = RoundedCornerShape(100.dp),
                    )
            )
            Image(
                modifier = Modifier.align(Alignment.Center),
                painter = painterResource(id = R.drawable.ic_app_icon),
                contentDescription = null,
                contentScale = ContentScale.Inside,
            )
        }

        Text(
            text = stringResource(id = R.string.splash_text1),
            fontFamily = SplashFont,
            fontSize = 11.sp,
            color = White50,
            fontWeight = FontWeight.Black,
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .systemBarsPadding()
                .padding(bottom = 10.dp)
        )
    }
}